﻿#include "../include/cat-proc.hpp"
#include "../include/cat-signal.hpp"

#include "../include/header/cat-config.hpp"

#include "../include/cat.hpp"
#include "../include/cat-hook.hpp"
#include <SFML/Window.hpp>


void cat_window_onproc(cat_window_t* _cat_window)
{
    sf::Event event; 
    while (_cat_window->window->pollEvent(event))
        switch (event.type)
        {
        case sf::Event::Closed:
            on_proc_close(_cat_window);
            break;
        case sf::Event::Resized: {                    
            cat_data_t* config = cat_load_default_config();
            config->json["main_decoration"]["window_size"][0]= event.size.width;
            config->json["main_decoration"]["window_size"][1]= event.size.height;
            cat_push_signal(UIWM_WINDOWRESIZE);
            break; 
        }
        case sf::Event::RawKeyPressed: {
            const cat_key_event keyevent = { event.rawKey.vkcode };
            cat_send_key_press(&keyevent, false);
            break;
        }
        case sf::Event::RawKeyReleased: {
            const cat_key_event keyevent = { event.rawKey.vkcode };
            cat_send_key_press(&keyevent, true);
            break;
        }
        case sf::Event::RawMouseButtonPressed: {
            const cat_mouse_click_event MouseClickEvent = { (cat_mouse_button_type)event.mouseButton.button };
            cat_send_mouse_click(&MouseClickEvent, false);
            break;
        }
        case sf::Event::RawMouseButtonReleased: {
            const cat_mouse_click_event MouseClickEvent = { (cat_mouse_button_type)event.mouseButton.button };
            cat_send_mouse_click(&MouseClickEvent, true);
            break;
        }
        case sf::Event::RawMouseMove: {
            const cat_mouse_move_event mouseevent = { event.mouseMove.x, event.mouseMove.y };
            cat_send_mouse_move(&mouseevent);
            break;
        }
        case sf::Event::RawMouseRelativeMove: {
            const cat_mouse_move_event mouseevent = { event.mouseMove.x, event.mouseMove.y };
            cat_send_force_mouse_move(&mouseevent);
            break;
        }      
        default: 
            break;
        }        
}

void on_proc_close(cat_window_t* _cat_window)
{
	cat_close_window(_cat_window);
}

void on_proc_resize(cat_window_t* _cat_window, int width, int height)
{
	cat_resize_window(_cat_window, width, height);
}
